<?php

declare (strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Model;

use App\Utils\Result;

/**
 * @property int $id 
 * @property string $node_name 
 * @property string $control_name 
 * @property string $action_name 
 * @property int $is_menu 
 * @property int $parent_id
 * @property string $style 
 */
class Node extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'node';
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'is_menu' => 'integer', 'parent_id' => 'integer'];

    /**
     * 获取节点数据
     */
    public function getNodeInfo($id,Role $role)
    {
        $field = explode(',','id,node_name,parent_id');
        $result = $this->select($field)->get();
        $str = '';

        $rule = $role->getRuleById($id);

        if(!empty($rule)){
            $rule = explode(',', $rule);
        }

        foreach($result as $key=>$vo){
            $str .= '{ "id": "' . $vo['id'] . '", "pId":"' . $vo['parent_id'] . '", "name":"' . $vo['node_name'].'"';

            if(!empty($rule) && in_array($vo['id'], $rule)){
                $str .= ' ,"checked":1';
            }

            $str .= '},';

        }

        return '[' . rtrim($str, ',') . ']';
    }

    /**
     * 根据节点数据获取对应的菜单
     * @param $nodeStr
     */
    public function getMenu($nodeStr = '')
    {
        if(empty($nodeStr)){
            return [];
        }
        // 超级管理员没有节点数组 * 号表示
        $where = '*' == $nodeStr ? 'is_menu = 1' : 'is_menu = 1 and id in(' . $nodeStr . ')';

        $field = explode(',','id,node_name,parent_id,sort,control_name,action_name,style,is_menu');
        $result = $this->orderBy('sort','desc')->whereRaw($where)->select($field)->get();

        return $result;
    }

    /**
     * 根据条件获取访问权限节点数据
     * @param $where
     */
    public function getActions($where)
    {
        $field = explode(',','control_name,action_name');
        return $this->where($where)->select($field);
    }

    /**
     * 获取节点数据
     * @return mixed
     */
    public function getNodeList($where=[])
    {
        $list = $this->orderBy('sort','desc')->where($where)->select(explode(',','id,sort,node_name as title,type,parent_id as pid,is_menu,style,control_name,action_name,type'))->get();
        return $list;
    }

    /**
     * 获取原始节点数据
     * @return mixed
     */
    public function getOriginalNodeList($where=[])
    {
        $list = $this->orderBy('sort','desc')->where($where)->select(explode(',','id,sort,node_name,type,parent_id,is_menu,style,control_name,action_name,type'))->get();
        return !empty($list)?$list->toArray():[];
    }

    /**
     * 获取权限节点数据
     * @return mixed
     */
    public function getPowerList($authNode)
    {
        //先获取所有节点
        $list = $this->orderBy('sort','desc')->select(explode(',','id,sort,node_name as title,parent_id as pid,is_menu,style,control_name,action_name,type'))->get();
        //再判断角色权限节点
        foreach ($list as $k => $v){
            if(in_array($v['id'],$authNode)){
                $list[$k]['checked'] = true;
            }else{
                $list[$k]['checked'] = false;
            }
        }
        return $list;
    }
    /**
     * 插入节点信息
     * @param $param
     */
    public function insertNode($param)
    {
        $res = $this->insertGetId($param);
        return $res;

    }

    /**
     * 多条记录插入
     * @param $params
     * @return bool
     */
    public function insertMultiple($params)
    {
        return $this->insert($params);
    }

    /**
     * 编辑节点
     * @param $param
     */
    public function editNode($param)
    {
        try{

            $res = $this->where('id' ,$param['id'])->update($param);
            return Result::return([],$res,'编辑节点成功');
        }catch(PDOException $e){
            return Result::data_error($e->getMessage());
        }
    }

    /**
     * 删除节点
     * @param $id
     */
    public function delNode($id)
    {
        try{

            $res = $this->where('id', $id)->delete();
            return Result::return([],$res,'删除节点成功');

        }catch(PDOException $e){
            return Result::data_error($e->getMessage());
        }
    }
}